package com.zt.rongda.salary.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.rongda.constant.BusConstant;
import com.zt.rongda.salary.inter.SalaryEmpBasicMapper;
import com.zt.rongda.salary.model.SalaryEmpBasic;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class SalaryEmpBasicService extends BaseService {

    @Autowired
    private SalaryEmpBasicMapper salaryEmpBasicMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
		sqlBuilder.appendSql("select seb.*,e.EMP_NAME from SALARY_EMP_BASIC seb ");
		sqlBuilder.appendSql(" LEFT JOIN EMPLOYEE e on e.EMP_ID=seb.EMP_ID where 1=1 ");
		sqlBuilder.addWhereLike2("e.EMP_NAME", reqBean.getInfo("empName"));
        JSONObject resultJson =  queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
        JSONArray rows = resultJson.getJSONArray("rows");
        for (int i = 0; i < rows.size(); i++) {
        	JSONObject json = rows.getJSONObject(i);
			String empId = json.getString("empId");
			StringBuffer sql = new StringBuffer("SELECT sep.EMP_ID,sep.EMP_QUOTA,wt.work_type_name  FROM SALARY_EMP_QUOTA sep");
			sql.append(" LEFT JOIN WORK_TYPE wt on wt.WORK_TYPE_ID=sep.WORK_TYPE_ID");
			sql.append(" WHERE sep.EMP_ID= ? ");
			JSONArray empQuotaArray = query(sql.toString(), empId);
			json.put("empQuotaArray", empQuotaArray);
		}
        return resultJson;
    }

    /**
    * 通过id查询
    *
    * @param empId
    * @return
    */
    public SalaryEmpBasic getById(String empId) {
        return salaryEmpBasicMapper.selectByPrimaryKey(empId);
    }

    /**
    * 新增
    *
    * @param salaryEmpBasic
    */
    public void add(SalaryEmpBasic salaryEmpBasic) {
        salaryEmpBasic.setEmpId(stringId());// 手动设置Id

        salaryEmpBasic.setMakeTime(new Date());
        salaryEmpBasic.setModifyTime(salaryEmpBasic.getMakeTime());
        salaryEmpBasicMapper.insert(salaryEmpBasic);
    }

    /**
    * 修改
    *
    * @param salaryEmpBasic
    */
    public void update(SalaryEmpBasic salaryEmpBasic) {
        String empId = salaryEmpBasic.getEmpId();
        if (StringUtil.isEmpty(empId)) {
            throw new BusinessException("获取主键失败");
        }

        salaryEmpBasic.setModifyTime(new Date());
        salaryEmpBasicMapper.updateByPrimaryKeySelective(salaryEmpBasic);
    }

    /**
    * 删除
    *
    * @param empId
    */
    public void delete(String empId) {
        if (StringUtil.isEmpty(empId)) {
            throw new BusinessException("获取主键失败");
        }
        salaryEmpBasicMapper.deleteByPrimaryKey(empId);
    }
    

	/**
	 * 新增一个员工，生成员工对应的工资项
	 * 
	 * @param empId
	 * @param userId
	 */
    public void createEmpBasic(String empId,String userId){
    	 Date date = new Date();
    	 SalaryEmpBasic salaryEmpBasic = new SalaryEmpBasic();
         salaryEmpBasic.setEmpId(empId);
         salaryEmpBasic.setBaseSalary(0.0);
         salaryEmpBasic.setWorkingAgeSubsidy(0.0);
         salaryEmpBasic.setPositionSalary(0.0);
         salaryEmpBasic.setSocialSecurity(0.0);
         salaryEmpBasic.setAttendance(0.0);
         salaryEmpBasic.setOperator(userId);
         salaryEmpBasic.setMakeTime(date);
         salaryEmpBasic.setModifyTime(date);
         salaryEmpBasicMapper.insert(salaryEmpBasic);
    }

}